package com.example.basicknowledge.设置圆角;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;

import com.example.basicknowledge.R;

public class RoundedCorner extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rounded_corner);

        Button button = (Button)findViewById(R.id.code_set_corner);
        // 外部矩形弧度
        float[] outerR = new float[] { 8, 8, 8, 8, 8, 8, 8, 8 };
        // 内部矩形与外部矩形的距离
        RectF inset = new RectF(20, 20, 15, 15);
        // 内部矩形弧度
        float[] innerRadii = new float[] { 20, 20, 20, 20, 20, 20, 20, 20 };

        RoundRectShape rr = new RoundRectShape(outerR, inset, innerRadii);
        ShapeDrawable drawable = new ShapeDrawable(rr);
        //指定填充颜色
        drawable.getPaint().setColor(Color.RED);
        // 指定填充模式
        drawable.getPaint().setStyle(Paint.Style.FILL);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            button.setBackground(drawable);
        }
        else {
            button.setBackgroundDrawable(drawable);
        }

        Button button1 = (Button)findViewById(R.id.code_set_corner_gradientdrawable);
        GradientDrawable drawable1 = new GradientDrawable();
        float radius = 20;
        drawable1.setCornerRadius(radius);
        drawable1.setStroke(2, 0xff333333);
        button1.setBackgroundDrawable(drawable1);
    }
}
